Is there a way to make this simpler?

I use the following code to do a string replace for certain characters in a buffer to make it XML compliant:
void convertXmlSymbols_(Buffer &b) {
int i = 0

// Reset buffers.
setempty(tempBuffer1)

for (i=0; i <= length(b); i++) {
// Strip any unnecessary characters
if (iscntrl(b[i]) && !isspace(b[i])) continue

if (i > 0) {
//This check is important to preserve non-ASCII symbols (of format \'b<n>).
if (bi:i == "'" && bi-1:i-1 != "\\" "") {
tempBuffer1 += "&apos;"
continue
}
} else {
if (bi:i == "'") {
tempBuffer1 += "&apos;"
continue
}
}
if (b[i] == '<'>') {
tempBuffer1 += ">"
continue
}
if (b[i] == '"') {
tempBuffer1 += """
continue
}
if (b[i] == '&') {
tempBuffer1 += "&"
continue
}
tempBuffer1 += b[i]
}

b = tempStringOf(tempBuffer1)
setempty(tempBuffer1)
}

The buffer tempBuffer1 is declared in the top context. I do it this way to ensure minimal memory leakage and optimal performance. Was wondering if there was a better way of writing this so I don't have to declare tempBuffer1 OR define a buffer within the function (which kills performance) or use strings (which kills memory).
SystemAdmin - Fri Nov 11 11:35:08 EST 2011

Re: Is there a way to make this simpler?
Mathias Mamsch - Fri Nov 11 14:30:35 EST 2011

Performance optimization is a very tricky business in DXL. You need to know a lot about the timing of basic operations, like a substring, allocation or a comparison and stuff like the string table and DOORS memory management to get optimal results. That being said the first thing I see is, that your code is far from optimal and you should not worry at the moment about performance, unless you are not processing large data sets (> 5MB) or you really want to optimize this.

You should have used the {co de} ... {co de} statement (without space) in your post to put the code in a readable block inside your post. Some stuff seems to be lost, however from what I can see you function is duplicating characters in a Buffer?

Declaring a special Buffer outside the function is indeed good practice in DXL. There is nothing wrong with it. Since DXL is single threaded you will never have the problem of the buffer being used by different threads. You only need one allocation. Your code is as readable as if you declared the buffer inside the function (in fact the declaration is 3-4 lines above where it would be inside the function), so nothing wrong with a tempbuffer here. I just would name it something like gTempBufConvertXMLSymbols then you do not need to fear some other gui declaring a tempbuffer in his code and the two libraries fighting against each other.

If you want to discuss details about optimizations to this code, just post ... Maybe that helps, Regards, Mathias

Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Is there a way to make this simpler?
SystemAdmin - Fri Nov 11 15:26:58 EST 2011

Mathias Mamsch - Fri Nov 11 14:30:35 EST 2011
Performance optimization is a very tricky business in DXL. You need to know a lot about the timing of basic operations, like a substring, allocation or a comparison and stuff like the string table and DOORS memory management to get optimal results. That being said the first thing I see is, that your code is far from optimal and you should not worry at the moment about performance, unless you are not processing large data sets (> 5MB) or you really want to optimize this.

You should have used the {co de} ... {co de} statement (without space) in your post to put the code in a readable block inside your post. Some stuff seems to be lost, however from what I can see you function is duplicating characters in a Buffer?

Declaring a special Buffer outside the function is indeed good practice in DXL. There is nothing wrong with it. Since DXL is single threaded you will never have the problem of the buffer being used by different threads. You only need one allocation. Your code is as readable as if you declared the buffer inside the function (in fact the declaration is 3-4 lines above where it would be inside the function), so nothing wrong with a tempbuffer here. I just would name it something like gTempBufConvertXMLSymbols then you do not need to fear some other gui declaring a tempbuffer in his code and the two libraries fighting against each other.

If you want to discuss details about optimizations to this code, just post ... Maybe that helps, Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

I'm working with some old code dating back a few years. What I wanted to do was rewrite the function without having to declare the buffer outside the function. I wanted to keep it all self-contained and return a string.

After some thought, I tried this approach -

string convertXmlSymbols_(Buffer b) {
    int i = 0
        
        for (i=0; i <= length(b); i++) {
                // Strip any unnecessary control characters (defined in ASCII decimal range of 0-31).
                if (iscntrl(b[i]) && !isspace(b[i])) continue
 
                // Replace XML control characters with XML entity.      
                if (b[i:i] == "'") {
                        if (i == 0) {
                                // If ' appears at the beginning of the buffer.
                                b = replaceRichText(tempStringOf(b), i, 1, "&apos;")
                        } else if (!(b[i-1:i-1] == "\\")) {
                                // If ' appears without a \ in front of it (denotes RTF control character.)
                                b = replaceRichText(tempStringOf(b), i, 1, "&apos;")
                        }                             
                } else if (b[i] == '<') {
                        b = replaceRichText(tempStringOf(b), i, 1, "<")
                } else if (b[i] == '>') {
                        b = replaceRichText(tempStringOf(b), i, 1, ">")
                } else if (b[i] == '"') {
                        b = replaceRichText(tempStringOf(b), i, 1, """)
                } else if (b[i] == '&') {
                        b = replaceRichText(tempStringOf(b), i, 1, "&amp;")
                }
        }
        
        return tempStringOf(b)
}

 


This function essential replaces ', ", &, <, and > characters that exist in the buffer with XML entity names (&apos;, ", etc.) This is to generate a text node that meets the XML requirements. The only exception is the \' case, which is an RTF identifier that gets screwed up by the RTF interpreter if it is written as \&apos;.

With all that being said, what I've written seems more complicated than it needs to be. I was hoping someone knew a better way of writing it without it impacting performance or memory. The previous post function works fine, it may not be optimal, but it does the job.

 

Re: Is there a way to make this simpler?
SystemAdmin - Fri Nov 11 15:37:23 EST 2011

SystemAdmin - Fri Nov 11 15:26:58 EST 2011

I'm working with some old code dating back a few years. What I wanted to do was rewrite the function without having to declare the buffer outside the function. I wanted to keep it all self-contained and return a string.

After some thought, I tried this approach -

string convertXmlSymbols_(Buffer b) {
    int i = 0
        
        for (i=0; i <= length(b); i++) {
                // Strip any unnecessary control characters (defined in ASCII decimal range of 0-31).
                if (iscntrl(b[i]) && !isspace(b[i])) continue
 
                // Replace XML control characters with XML entity.      
                if (b[i:i] == "'") {
                        if (i == 0) {
                                // If ' appears at the beginning of the buffer.
                                b = replaceRichText(tempStringOf(b), i, 1, "&apos;")
                        } else if (!(b[i-1:i-1] == "\\")) {
                                // If ' appears without a \ in front of it (denotes RTF control character.)
                                b = replaceRichText(tempStringOf(b), i, 1, "&apos;")
                        }                             
                } else if (b[i] == '<') {
                        b = replaceRichText(tempStringOf(b), i, 1, "<")
                } else if (b[i] == '>') {
                        b = replaceRichText(tempStringOf(b), i, 1, ">")
                } else if (b[i] == '"') {
                        b = replaceRichText(tempStringOf(b), i, 1, """)
                } else if (b[i] == '&') {
                        b = replaceRichText(tempStringOf(b), i, 1, "&amp;")
                }
        }
        
        return tempStringOf(b)
}

 


This function essential replaces ', ", &, <, and > characters that exist in the buffer with XML entity names (&apos;, ", etc.) This is to generate a text node that meets the XML requirements. The only exception is the \' case, which is an RTF identifier that gets screwed up by the RTF interpreter if it is written as \&apos;.

With all that being said, what I've written seems more complicated than it needs to be. I was hoping someone knew a better way of writing it without it impacting performance or memory. The previous post function works fine, it may not be optimal, but it does the job.

 

Note - noticed my code snippet didn't copy over well from my editor. Appears this environment converts the XML entities for ", <, and >.

Re: Is there a way to make this simpler?
Mathias Mamsch - Fri Nov 11 16:13:15 EST 2011

SystemAdmin - Fri Nov 11 15:26:58 EST 2011

I'm working with some old code dating back a few years. What I wanted to do was rewrite the function without having to declare the buffer outside the function. I wanted to keep it all self-contained and return a string.

After some thought, I tried this approach -

string convertXmlSymbols_(Buffer b) {
    int i = 0
        
        for (i=0; i <= length(b); i++) {
                // Strip any unnecessary control characters (defined in ASCII decimal range of 0-31).
                if (iscntrl(b[i]) && !isspace(b[i])) continue
 
                // Replace XML control characters with XML entity.      
                if (b[i:i] == "'") {
                        if (i == 0) {
                                // If ' appears at the beginning of the buffer.
                                b = replaceRichText(tempStringOf(b), i, 1, "&apos;")
                        } else if (!(b[i-1:i-1] == "\\")) {
                                // If ' appears without a \ in front of it (denotes RTF control character.)
                                b = replaceRichText(tempStringOf(b), i, 1, "&apos;")
                        }                             
                } else if (b[i] == '<') {
                        b = replaceRichText(tempStringOf(b), i, 1, "<")
                } else if (b[i] == '>') {
                        b = replaceRichText(tempStringOf(b), i, 1, ">")
                } else if (b[i] == '"') {
                        b = replaceRichText(tempStringOf(b), i, 1, """)
                } else if (b[i] == '&') {
                        b = replaceRichText(tempStringOf(b), i, 1, "&amp;")
                }
        }
        
        return tempStringOf(b)
}

 


This function essential replaces ', ", &, <, and > characters that exist in the buffer with XML entity names (&apos;, ", etc.) This is to generate a text node that meets the XML requirements. The only exception is the \' case, which is an RTF identifier that gets screwed up by the RTF interpreter if it is written as \&apos;.

With all that being said, what I've written seems more complicated than it needs to be. I was hoping someone knew a better way of writing it without it impacting performance or memory. The previous post function works fine, it may not be optimal, but it does the job.

 

Ouch, I think that approach is much worse than the first one, from a performance perspective. But something else first:

The code you have posted did not look like valid DXL. I was totally confused that you seem to replace a character by itself, until I realized the forum has replaced the XML tags to valid characters while parsing your post. :-)

Anyway, the fastest way for replacing a bunch of expressions in a large string is a regular expression (that has to be global) which will find the position of your expressions very quickly in large strings. For strings with a low size (about < 1000 chars) it is much faster to iterate over them using the character by character approach. In both cases you will build up a new buffer and either allocate and return it (compare the times of replacing the contents of the old buffer by the time for the allocation!) or replace the contents of the old buffer.

Okay, now coming to the specific problems with that second approach:
  • there is no need to loop from i = 0 to length b. the last character of b is (length b - 1)
  • the loop will calculate (length b) for every character in the buffer (OUCH! That is slow for large buffers). Store the length in an integer and put that in the loop condition.
  • do not use a buffer substring b[i:i] in expressions. Not only will this create a temporary buffer and therefore leak memory, it is also much faster to extract a character.
  • do NOT use replaceRichText. Remember that will return a string! So it will create a large string for your string table everytime you replace a character (which will eat your memory alive!). And it will then also copy that complete string to your buffer b (also very slow).
  • Never return a tempStringOf string. This string is as the name says temporary and might vanish as soon as DXL decides to relocate the buffer memory (which you never know).
  • If you extract a character b[i], remember that DOORS is unicode (variable character length), so for large strings that operation will become very slow, as it will need to iterate through your string to find the right position. So at least do not do it 8 times when 1 time would be enough.

Now coming to self-contained-ness ... As I said, in DXL the best thing you can do is to use a global buffer that is used only in one function. You cannot avoid globals in DXL, so better learn to live with them ;-) It can really be considered good practice to do so, so for whatever reason you want self contained functions, you might want to rethink. If you really want a self contained function at all costs, then let the user pass two buffers to the function. One input buffer, one output buffer. Rebuilding the string to a new buffer is essential for performance in that algorithm, you cannot get around this without serious memory or performance issues. Maybe that gives you some insights into performance optimization in DXL. Regards, Mathias

Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Is there a way to make this simpler?
OurGuest - Mon Nov 14 09:57:03 EST 2011

An easier approach may be to change course and look at msxml.

You can search for reference within this forum.

Re: Is there a way to make this simpler?
SystemAdmin - Mon Nov 14 11:12:18 EST 2011

OurGuest - Mon Nov 14 09:57:03 EST 2011
An easier approach may be to change course and look at msxml.

You can search for reference within this forum.

I considered using MSXML. However, it presents a couple of other issues. One of those being how to "pretty-print" the XML, rather than the long serialized output. The other is that it makes a mess of any RTF formatting you wish to preserve.